<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="8">
                <el-card shadow="hover" class="mgb20" style="height:252px;">
                    <div class="user-info">
                        <img src="../../assets/img/img.jpg" class="user-avator" alt />
                        <div class="user-info-cont">
                            <div class="user-info-name">{{name}}</div>
                            <div>{{role}}</div>
                        </div>
                    </div>
                </el-card>
                <el-card shadow="hover" style="height:355px;">
                    <div slot="header" class="clearfix">
                        <span>类型占比</span>
                    </div>指数型
                    <el-progress :percentage="percentData.exponential" color="#42b983"></el-progress>股票型
                    <el-progress :percentage="percentData.equity" color="#f1e05a"></el-progress>债券型
                    <el-progress :percentage="percentData.bondType"></el-progress>混合型
                    <el-progress :percentage="percentData.hybrid" color="#f56c6c"></el-progress>
                </el-card>
            </el-col>
            <el-col :span="16">
                <el-row :gutter="20" class="mgb20">
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-1">
                                <i class="el-icon-lx-people grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">1234</div>
                                    <div>用户访问量</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-2">
                                <i class="el-icon-lx-group grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">321</div>
                                    <div>用户数量</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-3">
                                <i class="el-icon-lx-recharge grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">5000</div>
                                    <div>交易总金额</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                </el-row>
                <el-card shadow="hover" style="height: 507px;">
                    <div class="handle-box" style="margin-bottom: 30px">
                        <span style="margin-right: 30px">基金信息</span>
                        <el-input placeholder="基金名称" v-model="query" class="handle-input mr10" style="width: auto"></el-input>
                        <el-button type="primary" icon="el-icon-search" @click="getData(1)">搜索</el-button>
                    </div>
                    <el-table
                            :data="tableData"
                            border
                            class="table"
                            ref="multipleTable"
                            header-cell-class-name="table-header"
                    >
                        <el-table-column type="index" :index="indexMethod" label="序号"></el-table-column>
                        <el-table-column prop="id" label="基金编号"></el-table-column>
                        <el-table-column prop="managerName" label="基金经理"></el-table-column>
                        <el-table-column prop="company" label="公司"></el-table-column>
                        <el-table-column prop="createDate" label="注册时间"></el-table-column>
                        <el-table-column prop="registeredCapital" label="资产规模"></el-table-column>
                        <el-table-column prop="type" label="类型"></el-table-column>
                        <el-table-column prop="name" label="基金名称"></el-table-column>
                    </el-table>
                    <div class="pagination">
                        <el-pagination
                                background
                                layout="total, prev, pager, next"
                                :page-size="pageSize"
                                :total="pageTotal"
                                @current-change="getData"
                        ></el-pagination>
                    </div>
                </el-card>
            </el-col>
        </el-row>
        <el-row :gutter="20">
            <el-col :span="12">
                <el-card shadow="hover">
                    <schart ref="bar" class="schart" canvasId="bar" :options="options"></schart>
                </el-card>
            </el-col>
            <el-col :span="12">
                <el-card shadow="hover">
                    <schart ref="line" class="schart" canvasId="line" :options="options2"></schart>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import Schart from 'vue-schart';
import bus from '../common/bus';
export default {
    name: 'dashboard',
    data() {
        return {
            name: localStorage.getItem('ms_username'),
            currentPage: 1,
            pageTotal: 3,
            pageSize: 5,
            percentData:{
                hybrid: 10,
                exponential: 40,
                equity: 30,
                bondType: 20,
            },
            tableData: [
                {
                    id: 1,
                    managerName: '测试01',
                    company: '123',
                    createDate: '2019-10-1',
                    registeredCapital: 100000.00,
                    type: '指数型',
                    name: '基金名称'
                },{
                    id: 1,
                    managerName: '测试02',
                    company: '123',
                    createDate: '2019-10-1',
                    registeredCapital: 100000.00,
                    type: '指数型',
                    name: '基金名称'
                },{
                    id: 1,
                    managerName: '测试03',
                    company: '123',
                    createDate: '2019-10-1',
                    registeredCapital: 100000.00,
                    type: '指数型',
                    name: '基金名称'
                },
            ],
            query: '',
            options: {
                type: 'line',
                title: {
                    text: ''
                },
                labels: [],
                datasets: [
                    {
                        label: '单位净值',
                        data: []
                    }
                ]
            },
            options2: {
                type: 'line',
                title: {
                    text: ''
                },
                labels: [],
                datasets: [
                    {
                        label: '单位净值',
                        data: []
                    }
                ]
            }
        };
    },
    components: {
        Schart
    },
    computed: {
        role() {
            return this.name === 'admin' ? '超级管理员' : '普通用户';
        }
    },
    created() {
        this.getData(1);
        this.getChartData(this.options,1);
        this.getChartData(this.options2,2);
    },
    methods: {
        indexMethod(index) {
            const _index = index+1+(this.currentPage-1)*this.pageSize;
            return _index;
        },
        // 获取后台的模拟数据
        getData(cpage) {
            const _this = this;
            this.currentPage = cpage;
            this.$http.get(`/fundSearch/${cpage}/${this.pageSize}?query=${this.query}`).then(function (resp) {
                _this.pageTotal = resp.data.total;
                _this.tableData = resp.data.list;
            });
        },
        async getChartData(option,fundId){
            const {data:resp} = await this.$http.get(`/fund_info/getChange/${fundId}`);
            this.fillChart(option,resp);
        },
        fillChart(option,data){
            option.title.text=data[0].fundName;
            for (let i = 0; i < data.length; i++) {
                option.labels.push(data[i].date);
                option.datasets[0].data.push(data[i].nav);
            }
        },
        changeDate() {
            const now = new Date().getTime();
            this.data.forEach((item, index) => {
                const date = new Date(now - (6 - index) * 86400000);
                item.name = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
            });
        }
    }
};
</script>


<style scoped>
.el-row {
    margin-bottom: 20px;
}

.grid-content {
    display: flex;
    align-items: center;
    height: 100px;
}

.grid-cont-right {
    flex: 1;
    text-align: center;
    font-size: 14px;
    color: #999;
}

.grid-num {
    font-size: 30px;
    font-weight: bold;
}

.grid-con-icon {
    font-size: 50px;
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    color: #fff;
}

.grid-con-1 .grid-con-icon {
    background: rgb(45, 140, 240);
}

.grid-con-1 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
    background: rgb(100, 213, 114);
}

.grid-con-2 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-3 .grid-con-icon {
    background: rgb(242, 94, 67);
}

.grid-con-3 .grid-num {
    color: rgb(242, 94, 67);
}

.user-info {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    border-bottom: 2px solid #ccc;
    margin-bottom: 20px;
}

.user-avator {
    width: 120px;
    height: 120px;
    border-radius: 50%;
}

.user-info-cont {
    padding-left: 50px;
    flex: 1;
    font-size: 14px;
    color: #999;
}

.user-info-cont div:first-child {
    font-size: 30px;
    color: #222;
}

.user-info-list {
    font-size: 14px;
    color: #999;
    line-height: 25px;
}

.user-info-list span {
    margin-left: 70px;
}

.mgb20 {
    margin-bottom: 20px;
}

.todo-item {
    font-size: 14px;
}

.todo-item-del {
    text-decoration: line-through;
    color: #999;
}

.schart {
    width: 100%;
    height: 300px;
}
</style>
